package com.xh.mybatisplus.config;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

public class RedissonManager {
    private static Config config = new Config();
    //声明redisso对象
    private static Redisson redisson = null;


    //获取redisson对象的方法
    public static void tryLock(String lockKey, long waitTimeout, long leaseTime) {
        // 1.构造redisson实现分布式锁必要的Config
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(0);
        // 2.构造RedissonClient
        RedissonClient redissonClient = Redisson.create(config);
        // 3.获取锁对象实例（无法保证是按线程的顺序获取到）
        RLock rLock = redissonClient.getLock(lockKey);
        try {
            /**
             * 4.尝试获取锁
             * waitTimeout 尝试获取锁的最大等待时间，超过这个值，则认为获取锁失败
             * leaseTime   锁的持有时间,超过这个时间锁会自动失效（值应设置为大于业务处理的时间，确保在锁有效期内业务能处理完）
             */
            boolean res = rLock.tryLock(waitTimeout, leaseTime, TimeUnit.SECONDS);
            if (res) {
                //成功获得锁，在这里处理业务
				System.out.println("redission ok lock,do sth...");
            }
        } catch (Exception e) {
            throw new RuntimeException("aquire lock fail");
        } finally {
            //无论如何, 最后都要解锁
            rLock.unlock();
        }
    }
    public static RedissonClient getRedission(){
        // 1.构造redisson实现分布式锁必要的Config
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(0);
        // 2.构造RedissonClient
       return Redisson.create(config);
    }


    public static void main(String[] args) {
        RedissonClient redission = RedissonManager.getRedission();
        RLock lock001 = redission.getLock("lock001");
        RLock lock002 = redission.getLock("lock001");

        System.out.println("第一次:"+lock001.tryLock());
        System.out.println("第二次:"+lock002.tryLock());
//        redission.getLock("lock001", 1000, 50000);
//        rLock.tryLock()
        System.out.println("over");
    }
}
